$(function () {
    new Vue({
        el:'#header-avatar',
        data:{
            staff:''
        },
        components: {
            'avatar': Avatar.Avatar
        },
        created() {
            this.$http.get('report/initEndReportRecords').then(function (res) {
                console.log(res.body);
                let result = res.body.data;
                this.staff = result.staff.name;
            }, function () {
                console.log('请求失败！');
            });
        }
    });
    new Vue({
        el: '#app',
        data: {
            staff: {},
            localAuth: {},
            password: {old:'', new:'', confirm:''}
        },
        methods: {
            save() {
                this.$http.post('user/updateProfile',
                    {id: this.staff.id, name:this.staff.name, phone: this.staff.phone, email: this.staff.email},{emulateJSON:true}).then(function (res) {
                    console.log(res.body);
                    if (res.body.success) {
                        alert("修改成功！")
                        console.log("修改成功！");
                    } else {
                        console.log("修改失败")
                    }
                },function(){
                    console.log('请求失败处理');
                });
            },
            updatePassword() {
                console.log(this.password);
                if (this.password.old !== this.localAuth.password) {
                    alert("旧密码错误！");
                    return 0;
                }
                if (this.password.new === '') {
                    alert("密码不允许为空！");
                    return 0;
                }
                if (this.password.new !== this.password.confirm) {
                    alert("两次输入的密码不一致！");
                    return 0;
                }
                this.$http.post('user/password',
                    {password: this.password.new},{emulateJSON:true}).then(function (res) {
                    console.log(res.body);
                    if (res.body.success) {
                        window.location.href = "backLogin";
                    } else {
                        console.log("修改失败")
                    }
                },function(){
                    console.log('请求失败处理');
                });
            }
        },
        created() {
            this.$http.get('user/getStaffInfo').then(function (res) {
                console.log(res.body);
                let result = res.body.data;
                this.staff = result.staff;
                this.localAuth = result.localAuth;
            }, function () {
                console.log('请求失败！');
            });
        }
    })
    $.get('report/initEndReportRecords', function (data) {
        let department = data.data.department;
        $('#department-name').text(department.deptName+'后台管理');
    });
    $('.logout').click(function () {
        $.get('user/logout', function (data) {

        }) ;
        window.location.href = 'backLogin.action';
    });
})